package com.gali.leetcode.day4;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: gali
 * @Date: 2022/7/17-07-17-20:43
 * @Description:整数转罗马数字---贪心算法
 */
public class Roman {
    int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    public String intToRoman(int num) {
        if (num==0){
            return " ";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            int value=values[i];
            String symbol = symbols[i];
            while (num>=value){
                num-=value;
                stringBuilder.append(symbol);
            }
        }
        return stringBuilder.toString();
    }
    public int romanToInt(String s) {
        Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }};
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            int value = symbolValues.get(s.charAt(i));
            if (i < n - 1 && value < symbolValues.get(s.charAt(i + 1))) {
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Roman roman = new Roman();
        roman.romanToInt("III");
    }
}
